package com.tycmc.signelectroniccontracts.tools.adapter.recyclerview;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class GeneralRecyclerAdapter<T> extends RecyclerView.Adapter<GeneralViewHolder> {
    private List<T> mList = new ArrayList<>();//数据源
    private Context mContext;
    private OnItemClickListener onItemClickListener;

    /**
     * 设置点击事件
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 构造方法
     *
     * @param context
     */
    public GeneralRecyclerAdapter(Context context) {
        mContext = context;
    }

    /**
     * 构造方法和list
     *
     * @param mContext
     * @param mList
     */
    public GeneralRecyclerAdapter(Context mContext, List<T> mList) {
        this.mList.addAll(mList);
        this.mContext = mContext;
    }

    /**
     * 添加对象
     *
     * @param entity
     */
    public void add(T entity) {
        if (entity == null) {
            return;
        }
        mList.add(entity);
        notifyItemChanged(mList.size() - 1);
    }

    public void add(T entity,int postion){
        if (entity == null) {
            return;
        }
        mList.add(postion, entity);
        notifyItemRangeChanged(postion,mList.size() );
    }
    /**
     * 添加集合对象
     *
     * @param list
     */
    public void addAll(List<T> list) {
        int start = mList.size();
        if (list == null) {
            return;
        }
        mList.addAll(list);
        notifyItemRangeChanged(start, mList.size());
    }

    public void addAll(List<T> list,int position) {
        if (list == null) {
            return;
        }
        mList.addAll(position,list);
        notifyItemRangeChanged(position, mList.size());
    }


    /**
     * 替换集合
     *
     * @param list
     */
    public void replaceAll(List<T> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 删除对象
     *
     * @param entity
     */
    public void delete(T entity) {
        if (mList.contains(entity)) {
            int indexOf = mList.indexOf(entity);
            mList.remove(entity);
            notifyItemRemoved(indexOf);
        }
    }

    /**
     * 删除某一个位置的对象
     *
     * @param positon
     */
    public void delete(int positon) {
        mList.remove(positon);
        notifyItemRemoved(positon);
    }

    /**
     * 获取整个list集合
     *
     * @return
     */
    public List<T> getmList() {
        return mList;
    }

    /**
     * 获取某个位置的对象
     *
     * @param postion
     * @return
     */
    public T getItem(int postion) {
        return mList.get(postion);
    }

    @NonNull
    @Override
    public GeneralViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        int layoutId = getView(type);
        if (layoutId == -1) {
            return null;
        }
        View inflate = LayoutInflater.from(mContext.getApplicationContext()).inflate(layoutId, viewGroup, false);
        return GeneralViewHolder.getViewHolder(mContext, inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull GeneralViewHolder generalViewHolder, final int position) {
        convert(generalViewHolder, mList.get(position));
        if (generalViewHolder != null) {
            if (generalViewHolder.getConvertView() != null) {
                generalViewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick(view, GeneralRecyclerAdapter.this, position);
                        }
                    }
                });
                generalViewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        if (onItemClickListener != null) {
                            return onItemClickListener.onItemLongClick(view, GeneralRecyclerAdapter.this, position);
                        }
                        return false;
                    }
                });
            }

        }


    }

    @Override
    public int getItemViewType(int position) {
        return getViewType(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public abstract int getViewType(T entity);

    public abstract int getView(int type);

    public abstract void convert(GeneralViewHolder helper, T item);

    public interface OnItemClickListener {
        void onItemClick(View view, GeneralRecyclerAdapter adapter, int postion);

        boolean onItemLongClick(View view, GeneralRecyclerAdapter adapter, int postion);
    }

}
